using UnityEngine;

public class BtnDistrictRespectBattle : ActivatableButton
{
	[SerializeField]
	private MapDistrict district;

	[SerializeField]
	private UIButton button;

	[SerializeField]
	private UISprite sprite;

	public Color currentColor = Color.white;

	private bool mapAvailable = true;

	private bool networkAvailable = true;

	private void Awake()
	{
		district = GetComponentInParent<MapDistrict>();
		ReputationInterfaceController.networkAvailable += NetworkAvailableEventHandler;
		MapDataDownloader.MapsDataUpdated += MapsDataUpdatedEventHandler;
		button = GetComponent<UIButton>();
		if (button != null && button.tweenTarget != null)
		{
			sprite = button.tweenTarget.GetComponent<UISprite>();
		}
		networkAvailable = PhotonNetwork.connected;
		SwitchButtonSate(networkAvailable && mapAvailable);
	}

	private void OnEnable()
	{
		UpdateColor();
		if (district != null && MapDataDownloader.instance != null)
		{
			MapsDataUpdatedEventHandler();
		}
	}

	private void OnDestroy()
	{
		ReputationInterfaceController.networkAvailable -= NetworkAvailableEventHandler;
		MapDataDownloader.MapsDataUpdated -= MapsDataUpdatedEventHandler;
	}

	public void MapsDataUpdatedEventHandler()
	{
		if (MapDataDownloader.instance != null)
		{
			MapDataDownloader.instance.mapData.TryGetValue(district.nameScene, out mapAvailable);
		}
		BCWDebug.LogError("MapsDataUpdatedEventHandler");
		SwitchButtonSate(mapAvailable);
		UpdateColor();
	}

	public void NetworkAvailableEventHandler(bool active)
	{
		networkAvailable = active;
		if (mapAvailable)
		{
			SwitchButtonSate(networkAvailable);
			UpdateColor();
		}
	}

	public void OnClick()
	{
		if (controllerMenu.thisScript != null)
		{
			if (!networkAvailable)
			{
				ReputationInterfaceController.instance.ShowNoInternet();
			}
			else if (!mapAvailable)
			{
				ReputationInterfaceController.instance.ShowMapUnAvailable();
			}
		}
		if (networkAvailable && mapAvailable)
		{
			BCWStorage.Set(settings.keyForShowLoadingSprite, district.nameScene);
			district.LoadLevel();
		}
	}

	public void SwitchButtonSate(bool active)
	{
		if (active)
		{
			currentColor = Color.white;
		}
		else
		{
			currentColor = Color.grey;
		}
		UpdateColor();
	}

	private void UpdateColor()
	{
		if (sprite != null)
		{
			sprite.color = currentColor;
		}
		if (button != null)
		{
			button.defaultColor = currentColor;
			button.UpdateColor(true);
		}
	}
}
